app.controller("defectCtrl", function ($scope, $http, $window, idService, proService, userService) {

    // user
    $scope.principle = {};
    $scope.isManager = false;
    $scope.isTester = false;
    $scope.isDeveloper = false;
    $scope.isRequire = false;
    idService.principle().then(function(value) {$scope.principle = value.data;});
    idService.isManager().then(function(value) {$scope.isManager = value.data;});
    idService.isTester().then(function(value) {$scope.isTester = value.data;});
    idService.isDeveloper().then(function(value) {$scope.isDeveloper = value.data;});
    idService.isRequire().then(function(value) {$scope.isRequire = value.data;});

    // menu
    $scope.isTodo = true;
    $scope.isAdd = true;
    $scope.isDetail = false;
    $scope.isRevise = false;
    $scope.isMyStarted = false;
    $scope.isMyResolved = false;


    // 变量声明
    $scope.todoTaskList = [];
    $scope.comment = "";
    $scope.defect = {
        projectId: "",
        rank: 3,
        content: "",
        assignee: "",
        attachmentList: [],
        commentList: [],
        finished: false
    };
    $scope.defaultImg = "";
    $scope.proList = [];
    $scope.devList = [];
    $scope.preferPro = {};

    $scope.proList = proService.proList().then(
        function (res) {
            $scope.proList = res.data.data;
        }
    );
    userService.devList().then(function (res) {
        $scope.devList = res.data.data;
    });
    $scope.getPreferPro = function () {
        $http.get(
            "/project/prefer"
        ).then(function (value) {
            restResult(value, function (data) {
                $scope.preferPro = data;
            });
        })
    };


    $scope.addDefect = function () {
        var images = $(".attachedImg").val();
        $scope.defect.attachmentList = images.split(",");
        $http.post(
            "/defect/start",
            $scope.defect
        ).then(function (value) {
            restResult(value);
            $window.location.reload();
        })
    };
    $scope.editDefect = function (completed) {
        $scope.defect.completed = completed;
        $http.post(
            "/defect/revise",
            $scope.defect
        ).then(function (value) {
            restResult(value);
            $window.location.reload();
        })
    };
    $scope.getTodoTask = function () {
        $http.get(
            "/task/todo"
        ).then(function (resp) {
            restResult(resp, function (data) {
                $scope.todoTaskList = data;
            })
        })
    };
    $scope.setAssignee = function() {
        $http.post(
            "/set/assignee",
            $scope.defect
        ).then(function (value) {
            restResult(value);
            $window.location.reload();
        })
    };
    $scope.doTask = function (taskId, completed) {
        $http.post(
            "/do/task",
            {
                "taskId": taskId,
                "completed": completed
            }
        ).then(function (resp) {
            restResult(resp);
            $window.location.reload();
        })
    };

    //
    $scope.myStarted = function () {
        $http.get(
            "/his/started?first=0&size=1000"
        ).then(function (value) {
            restResult(value, function (data) {
                $scope.startedList = data;
            })
        })
    };
    $scope.myResolved = function () {
        $http.get(
            "/his/finished?first=0&size=1000"
        ).then(function (value) {
            restResult(value, function (data) {
                $scope.resolvedList = data;
            })
        })
    };



    $scope.addComment = function(taskId, comment) {
        var data = new FormData();
        data.append("taskId", taskId);
        data.append("comment", comment);
        $http({
            method: 'POST',
            url: '/comment/add',
            data: data,
            headers: { 'Content-Type': undefined }
        }).then(function (value) {
            restResult(value, function (data) {
                $scope.getComment(taskId);
                $scope.comment = "";
            });
        })
    };
    $scope.getComment = function(taskId) {
        $http.get(
            "/comment/list?taskId=" + taskId
        ).then(function (resp) {
            restResult(resp, function(data){
                $scope.defect.commentList = data;
            })
        })
    };

    $scope.setContentItems = function(defect) {
        var items = defect.content.split("\n");
        var contentItems = [];
        for(var i=0; i<items.length; i++){
            if (""!==items[i]) {
                contentItems.push(items[i]);
            }
        }
        $scope.defect.contentItems = contentItems;
    };


    $scope.reader = new FileReader();  //创建一个FileReader接口
    $scope.img_upload = function (files) {    //单次提交图片的函数
        $scope.guid = (new Date()).valueOf();  //通过时间戳创建一个随机数，作为键名使用
        $scope.reader.readAsDataURL(files[0]); //FileReader的方法，把图片转成base64
        $scope.reader.onload = function (ev) {
            $scope.$apply(function () {
                $(".paste-img").append("<img class='attach-img' src='"+ev.target.result+"' onclick='imgDetail(this)' data-target='#imgModal'/>");
            });
        };
        var data = new FormData();   //以下为像后台提交图片数据
        data.append('image', files[0]);
        data.append('guid', $scope.guid);
        data.append('taskId', $("#detailTaskId").val());
        $http({
            method: 'post',
            url: '/file/upload',
            data: data,
            headers: {'Content-Type': undefined},
            transformRequest: angular.identity
        }).then(function (resp) {
            var imagesVal = $(".attachedImg").val();
            $(".attachedImg").val(imagesVal + "," + resp.data.data.fileName);
        })
    };

    // menu跳转
    $scope.taskDetail = function(task) {
        $scope.canRevise = task.taskName === "修改缺陷";
        $scope.isDetail = true;
        $scope.isRevise = false;
        $scope.isAdd = false;
        $scope.isMyStarted = false;
        $scope.isMyResolved = false;
        $scope.defect = task;
        $scope.getComment(task.taskId);
        $scope.setContentItems(task);
    };
    $scope.toRevise = function() {
        $scope.isDetail = false;
        $scope.isRevise = true;
    };
    $scope.toAdd = function () {
        $scope.isAdd = true;
        $scope.isDetail = false;
        $scope.isRevise = false;
        $scope.isMyStarted = false;
        $scope.isMyResolved = false;
    };
    $scope.toMyStarted = function () {
        $scope.isAdd = false;
        $scope.isDetail = false;
        $scope.isRevise = false;
        $scope.isMyStarted = true;
        $scope.isMyResolved = false;
    };
    $scope.toMyResolved = function () {
        $scope.isAdd = false;
        $scope.isDetail = false;
        $scope.isRevise = false;
        $scope.isMyStarted = false;
        $scope.isMyResolved = true;
    };
});
